class PlayerAPI {
    constructor(player) {
        this.player = player;
    }

    play() {
        return this.player.video.play().catch(error => {
            console.error('播放失败:', error);
            this.player.showError('播放失败，请检查网络连接');
        });
    }

    pause() {
        this.player.video.pause();
    }

    seek(time) {
        if (this.player.video.readyState > 0) {
            this.player.video.currentTime = time;
        }
    }

    setVolume(volume) {
        this.player.video.volume = Math.min(Math.max(volume, 0), 1);
    }

    toggleFullscreen() {
        if (!document.fullscreenElement) {
            this.player.container.requestFullscreen()
                .catch(err => console.error('全屏失败:', err));
        } else {
            document.exitFullscreen();
        }
    }

    formatTime(seconds) {
        if (isNaN(seconds)) return '00:00';
        const minutes = Math.floor(seconds / 60);
        const secs = Math.floor(seconds % 60);
        return `${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
    }

    getVideoFormat() {
        const src = this.player.options.src.toLowerCase();
        if (src.includes('.flv')) return 'FLV';
        if (src.includes('.m3u8')) return 'HLS';
        if (src.includes('.mp4')) return 'MP4';
        if (src.includes('.webm')) return 'WebM';
        return 'Unknown';
    }
}

export default PlayerAPI; // 唯一导出